import {createRouter, createWebHistory} from 'vue-router'
import Base from '../views/Base.vue'
import store from "@/store";

const routes = [
  {
    meta: {
      title: 'uric自动化运维平台',
      authenticate: false
    },
    path: '/uric',
    alias: '/',
    name: 'Base',
    component: Base,
    children: [
      {
        meta: {
          title: '展示中心',
          authenticate: false
        },
        path: 'show_center',
        alias: '',
        name: 'ShowCenter',
        component: () => import(/* webpackChunkName: "about" */ '../views/ShowCenter.vue')
      },
      {
        meta: {
          title: '资产管理',
          authenticate: true
        },
        path: 'host',
        name: 'Host',
        component: () => import(/* webpackChunkName: "about" */ '../views/HostView.vue')
      },
      {
        meta: {
          title: 'Console',
          authenticate: true
        },
        path: 'console/:id',
        name: 'Console',
        component: () => import('../views/ConsoleView.vue')
      },
      {
        path: 'multi_exec',
        name: 'MultiExec',
        component: () => import('../views/MultiExec.vue')
      },
      {
        path: 'environment',
        name: 'Environment',
        component: () => import('../views/Environment.vue')
      },
      {
        path: 'release',
        name: 'Release',
        component: () => import('../views/Release.vue')
      },
      {
        path: 'git',
        name: 'Git',
        component: () => import('../views/Git.vue')
      },
      {
        path: 'jenkins',
        name: 'Jenkins',
        component: () => import('../views/Jenkins.vue')
      },
      {
        path: 'schedule',
        name: 'Schedule',
        component: () => import('../views/Schedule.vue')
      },
      {
        path: 'monitor',
        name: 'Monitor',
        component: () => import('../views/Monitor.vue')
      }
    ]
  },
  {
    meta: {
      title: '登录',
      authenticate: false
    },
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 路由守卫，主要是编写一些在页面跳转过程中， 需要自动执行的代码。例如：修改页面头部标题，验证权限。
router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  console.log('to:::', to);
  console.log('from:::', from);
  console.log("token:::", store.getters.token);
  if (to.meta.authenticate && store.getters.token === '') {
    next({name: 'Login'})
  } else {
    next()
  }
});

export default router
